<?php
function utf8Encode($string)
{
	return $string = mb_convert_encoding($string, "utf-8", "euc-kr");
}
function weatherParser($items, $str) 
{
	$result = Array();
	foreach($items as $item) 
	{
		if (eregi("<$item data=\"([^\"]+)\"/>", $str, $reg))
		{
			$result[$item] = $reg[1];
		}
		else
		{
			$result[$item] = "";
		}
	}
	return $result;
}


function Weather_Show($target)
{
	$xmllink = "http://www.google.co.kr/ig/api?weather=";
	$cityname = "jeju";
	global $configVal;
    requireComponent('Textcube.Function.Setting');
    $data = setting::fetchConfigVal( $configVal);
    if( !is_null( $data ) ){
        $cityname = $data['city'];
    }   
	$xmllink .= $cityname;
	$xml_data= @file_get_contents($xmllink);
	//$xml_string = utf8Encode($xml_data);
	//print $xml_string;

	preg_match_all("|<[^>]+>(.*)</[^>]+>|U", $xml_data, $reg, PREG_SET_ORDER);

	$current_data = $reg[1][1];

	if ($current_data != '') 
	{
		$weatherData = Array();
		if ($current_data != '')
		{
			$weatherData['current'] = weatherParser(Array('condition','temp_f','temp_c','humidity','icon','wind_condition'), $current_data);	
		}
		$day_data = $reg[2][1];
		if ($day_data != '')
		{
			$weatherData['day0'] = weatherParser(Array('day_of_week','low','high','icon','condition'), $day_data);	
		}
		$day_data = $reg[3][1];
		if ($day_data != '')
		{
			$weatherData['day1'] = weatherParser(Array('day_of_week','low','high','icon','condition'), $day_data);	
		}
		$day_data = $reg[4][1];
		if ($day_data != '')
		{
			$weatherData['day2'] = weatherParser(Array('day_of_week','low','high','icon','condition'), $day_data);	
		}
		$day_data = $reg[5][1];
		if ($day_data != '')
		{
			$weatherData['day3'] = weatherParser(Array('day_of_week','low','high','icon','condition'), $day_data);
		}
		

		$txtResult = "<br/>";
		$txtResult = "<h3>날씨정보</h3>";
		$txtResult .= "<B>";
		$txtResult .= $cityname;
		$txtResult .= "&nbsp;날씨:</B>";
		$txtResult .= "&nbsp;";
		$txtResult .= "<img height=15 src=http://www.google.com/";
		$txtResult .= $weatherData[day0][icon]; 
		$txtResult .= ">";
		$txtResult .= "&nbsp;";
		$txtResult .= utf8Encode($weatherData[day0][condition]);
		$txtResult .= "&nbsp;/&nbsp;<br/>";
		$txtResult .= "최저기온:";
		$low = sprintf("%d", ($weatherData[day0][low]));
		$txtResult .=  $low;
		$txtResult .= "°C";
		$txtResult .= "&nbsp;/&nbsp;";
		$txtResult .= "최고기온:";
		$high = sprintf("%d", ($weatherData[day0][high]));
		$txtResult .=  $high;
		$txtResult .= "°C";
		$txtResult .= "&nbsp;<br>";
		$txtResult .= "<B>현재 날씨:</B>";
		$txtResult .= "&nbsp;"; 
		$txtResult .= "<img height=15 src=http://www.google.com/";
		$txtResult .= $weatherData[current][icon];
		$txtResult .= ">";
		$txtResult .= "&nbsp;";
		$txtResult .= utf8Encode($weatherData[current][condition]);
		$txtResult .= "&nbsp;/&nbsp;";
		$txtResult .= "<br/>";
		$txtResult .= "현재기온:";
		$txtResult .= "&nbsp;";
		$txtResult .= $weatherData[current][temp_c]; 
		$txtResult .= "°C";
		$txtResult .= "&nbsp;/&nbsp;";
		$txtResult .= "&nbsp;";
		$txtResult .= utf8Encode($weatherData[current][humidity]);
		$txtResult .= "&nbsp;";
		$txtResult .= "<br>";
		$txtResult .= utf8Encode($weatherData[current][wind_condition]);
		$txtResult .= "&nbsp;";
		$txtResult .= "<br>";
		$txtResult .= "<B>내일 날씨:</B>";
		$txtResult .= "&nbsp;";
		$txtResult .= "<img height=15 src=http://www.google.com/"; 
		$txtResult .= $weatherData[day1][icon];
		$txtResult .= ">";
		$txtResult .= "&nbsp;";
		$txtResult .= utf8Encode($weatherData[day1][condition]);
		$txtResult .= "<br/>";

		//$encResult = utf8Encode($txtResult)
		return $txtResult;
	}
}
?> 
<?php
       $target = ob_get_contents();
       ob_end_clean();
       return $target;
   }
   function TextcubeBirthdayDataSet($DATA){
       requireComponent('Textcube.Function.Setting');
       $cfg = setting::fetchConfigVal( $DATA );
       // if( $cfg['month'] != 날짜냐?) return "잘못된 날짜입니다.";
       // 등등등등 여기서 원하는 검증을 하고 검증 실패시 사용자에게 보여줄 에러메세지를 리턴하면 됩니다.
       // 성공하면 그냥 true
       return true;
   }
?>
